const request = require('request');
const debug = require('debug');
const config = require('../config');
const log = debug('viewdata:essearch');
module.exports = function (filter, callback, cfg = {}) {
  log('filter', filter)
  request({
    url: config.elasticsearch.url,
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      "query": {
        "bool": {
          "filter": filter,
          "should": [],
          "must_not": []
        }
      },
      "sort": [{ "@timestamp": { "order": "desc", "unmapped_type": "boolean" } }],
      "from": 0,
      "size": cfg.size || 50
    })
  }, callback)

}

/**
 *  [
            {
              "match_all": {}
            },
            {"match_phrase": {"message": "Mon Jun 22 16:49:01 CST 2020"}},
            {"match_phrase": {"message": "Mon Jun 22 16:49:01 CST 2020"}},
            {
              "range": {
                "@timestamp": {
                  "gte": "2020-06-22T07:01:41.101Z",
                  "lte": "2020-06-22T07:16:41.101Z",
                  "format": "strict_date_optional_time"
                }
              }
            }
          ]
{
  "query": {
    "bool": { // 联合查询
      "must": {
        "bool": {
          "should": [ // or关系
            {
              "match": {
                "about": "music"
              }
            },
            {
              "match": {
                "about": "climb"
              }
            }
          ]
        }
      },
      "must": {
        "match": {
          "first_nale": "John",
          "fuzziness": "AUTO" // fuzziness的值指定为AUTO，其在term的长度大于5的时候相当于指定值为2，然而80%的人拼写错误的编辑距离(edit distance)为1，所有如果你将fuzziness设置为1可能会提高你的搜索性能
        }
      },
      "must_not": {
        "match": {
          "last_name": "Smith"
        }
      },
      "multi_match": {
        "query": "rock",
        "fields": [
          "about",
          "interests"
        ]
      },
      "range": { // 范围查询
        "birthday": {
          "gte": "2017-02-01",
          "lte": "2017-05-01"
        }
      }
    },
    "filtered": { // 过滤查询
      "query": {
        "multi_match": {
          "query": "music",
          "fields": [
            "about",
            "interests"
          ]
        }
      },
      "filter": {
        "range": {
          "birthday": {
            "gte": "2017-02-01"
          }
        }
      }
    }
  },
  "_source": [
    "first_name",
    "last_name",
    "interests"
  ],
  "sort": [
    {
      "publish_date": {
        "order": "desc"
      }
    },
    {
      "id": {
        "order": "desc"
      }
    }
  ],
  "size": 1
}

 */
